﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DCSLibrary;
using Utils;
using Telerik.Web.UI;
using Utils.DC.Web.UI;

public partial class Admin_Module_phan_quyen_quan_tri : System.Web.UI.UserControl
{
    #region Cac bien dung chung
    UsersUI objUsers = new UsersUI();
    DataSet dsUsers;
    FunctionPageUI objPage = new FunctionPageUI();
    #endregion

    #region Cac Ham Su Kien

    protected void Page_Load(object sender, EventArgs e)
    {
        dsUsers = objUsers.GetByUserType((byte)Common.USER_TYPE.Admin);
        if (!IsPostBack)
        {
            BindGrid();
        }
    }

    #endregion

    #region Cac Ham Tuong Tac Du Lieu

    private void BindGrid()
    {
        grd.DataSource = dsUsers.Tables[0];
        grd.DataBind();
    }

    #endregion

    #region Cac ham khac

    protected string GetUserType(byte userType)
    {
        if (userType == Convert.ToByte(Utils.Common.USER_TYPE.NotSet))
        {
            return "Chưa xác định";
        }
        else if (userType == Convert.ToByte(Utils.Common.USER_TYPE.User))
        {
            return "Người dùng";
        }
        else if (userType == Convert.ToByte(Utils.Common.USER_TYPE.Admin))
        {
            return "Quản Trị";
        }
        return "";
    }
    #endregion

    protected void grd_PageIndexChanged(object source, GridPageChangedEventArgs e)
    {
        BindGrid();
    }

    protected void btnSetRole_Click(object sender, EventArgs e)
    {

    }

    protected void ddl1_SetCurrentRole(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList list = (DropDownList)sender;
            int userID = Convert.ToInt32(list.ToolTip);
            DataTable dt = Utils.DatabaseUtils.TableLib.CreateTable(dsUsers.Tables[0], "UserID = " + userID);
            if (dt != null && dt.Rows.Count > 0)
            {

                //Nếu người dùng chưa được cấp quyền thì set vào chưa xác định luôn, ko phải duyệt j nữa
                if (dt.Rows[0]["RoleID"] == null || dt.Rows[0]["RoleID"].ToString().Trim() == "")
                {
                    list.Items[0].Selected = true;
                    return;
                }

                foreach (ListItem item in list.Items)
                {
                    if (Convert.ToInt32(item.Value) == Convert.ToInt32(dt.Rows[0]["RoleID"]))
                    {
                        item.Selected = true;
                    }
                }
            }
        }
    }

    protected DataTable LoadRole()
    {
        DataTable dt = new RoleUI().GetAll().Tables[0];
        DataRow row = dt.NewRow();
        row["RoleID"] = -1;
        row["RoleName"] = "Chưa thuộc nhóm";
        dt.Rows.InsertAt(row,0);
        return dt;
    }

    protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;
        int userID = Convert.ToInt32(list.ToolTip);
        int result = objUsers.UpdateRole(userID, Convert.ToInt32(list.SelectedValue));
        if (result > 0)
        {
            Response.Write(new MessageBox().Alert(MessageBox.MESSAGE_ADD_SUCCESSFULLY));
            
        }
        else
        {
            Response.Write(new MessageBox().Alert(MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY));
            //lblMessage.Text = MessageBox.MESSAGE_ADD_NOT_SUCCESSFULLY;
        }
    }
}
